



#include <stdio.h>
#include <windows.h>

HANDLE mutex;

void PrintLine(const char *str) {
	for (int i=0; i<70; i++) {
		printf(str);
		Sleep(2);
	}
	printf("\r\n");
}


unsigned __stdcall Thread( const char *str )
{
	while (1) {
	
		WaitForSingleObject(mutex, INFINITE);
		
		PrintLine(str);

		WaitForSingleObject(mutex, INFINITE);
		
		
		PrintLine(str);
		

		ReleaseMutex(mutex);
		
		PrintLine(str);

		ReleaseMutex(mutex);
		
	}
	ExitThread(0);
}


/*class ICoreObject {
		virtual int	AddRef	(void) = 0;
		virtual int Release	(void) = 0;
	};
	

class ECoreObject : public ICoreObject {
	public:
				ECoreObject	(void);
				~ECoreObject	(void);
		virtual int	AddRef	(void);
		virtual int	Release	(void);
	private:
		int	ref_counter;
	};	


ECoreObject::ECoreObject()
{
	printf("ECoreObject::ECoreObject()\r\n");
	ref_counter	=	0;
}

ECoreObject::~ECoreObject(void)
{
	printf("ECoreObject::~ECoreObject()\r\n");
}

int	ECoreObject::AddRef(void)
{
	ref_counter++;
	return ref_counter;
}

int ECoreObject::Release(void)
{
	ref_counter--;
	if (ref_counter==0) {
		delete this;
	}
	return ref_counter;
}


void CreateCoreObject(ECoreObject **obj)
{
	*obj	=	new ECoreObject;
	(*obj)->AddRef();
}				  */


int main(int argc, const char **argv)
{
	mutex = CreateMutex(NULL, FALSE, NULL);

	HANDLE th1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, "a", 0, 0);
	HANDLE th2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, "b", 0, 0);
	HANDLE th3 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Thread, "c", 0, 0);//*/
	
	/*ECoreObject	*obj;
	CreateCoreObject(&obj);
	obj->Release();*/
		
	while (1) {
	};
	
	CloseHandle(mutex);
	
	CloseHandle(th3);
	CloseHandle(th2);
	CloseHandle(th1);//*/
}